 #include <iostream>
using namespace std;
int main ()
{int p,e,i,d,j;
 int k;
 for(int l=1;;l++)
 {
  cin>>p>>e>>i>>d;
  if(p==-1&&e==-1&&i==-1&&d==-1) break;
  for( j=d+1;j<21252;j++)
  {
   if((j-p)%23==0&&(j-e)%28==0&&(j-i)%33==0) break;
   }
  cout<<"Case "<<l<<": the next triple peak occurs in "<<j-d<<" days."<<endl;
  }
  system("pause");
  return 0;
}

